import argparse
import json
import time
import gradio as gr

from tqdm import tqdm
import os


def format_example(example: dict) -> dict:
    context = f"Instruction: {example['instruction']}\n"
    if example.get("input"):
        context += f"Input: {example['input']}\n"
    context += "Answer: "
    target = example["output"]
    return {"context": context, "target": target}


def convert(input,output,progress:gr.Progress = None):
    if  os.path.exists(output):
        os.remove(output)
    with open(input) as f:
        examples = json.load(f)

    with open(output, 'w') as f:
        if progress is not None:
            bar = progress.tqdm(examples, desc="formatting..")
        else:
            bar = tqdm(examples, desc="formatting..")
        for example in bar:
            f.write(json.dumps(format_example(example)) + '\n')


